using System;
using AndyPike.Commons.Extensions;
using NUnit.Framework;
using NUnit.Framework.SyntaxHelpers;

namespace AndyPike.Commons.Tests.Unit.Extensions
{
    [TestFixture]
    public class When_validating_an_email_address
    {
        [Test]
        public void Should_return_true_for_various_valid_email_address_formats()
        {
            Assert.That("someone@somewhere.com".IsValidEmailFormat(), Is.True);
            Assert.That("firstName.lastName@somewhere.com".IsValidEmailFormat(), Is.True);
            Assert.That("someone@somewhere.co.uk".IsValidEmailFormat(), Is.True);
            Assert.That("firstName.lastName@somewhere.co.uk".IsValidEmailFormat(), Is.True);
        }

        [Test]
        public void Should_return_false_for_various_invalid_email_address_formats()
        {
            Assert.That(((string)null).IsValidEmailFormat(), Is.False);
            Assert.That("".IsValidEmailFormat(), Is.False);
            Assert.That("blah".IsValidEmailFormat(), Is.False);
            Assert.That("blah@blah".IsValidEmailFormat(), Is.False);
            Assert.That("blah@.blah.com".IsValidEmailFormat(), Is.False);
            Assert.That("blah@blah@blah.com".IsValidEmailFormat(), Is.False);
        }
    }

    [TestFixture]
    public class When_checking_if_a_string_is_a_valid_guid
    {
        [Test]
        public void Should_return_true_for_a_valid_guids()
        {
            string validGuid = Guid.NewGuid().ToString();
            string emptyGuid = Guid.Empty.ToString();

            Assert.That(validGuid.IsGuid(), Is.True);
            Assert.That(emptyGuid.IsGuid(), Is.True);
        }

        [Test]
        public void Should_return_false_for_a_invalid_guids()
        {
            Assert.That(((string)null).IsGuid(), Is.False);
            Assert.That("".IsGuid(), Is.False);
            Assert.That("blah-blah-blah".IsGuid(), Is.False);
        }
    }

    [TestFixture]
    public class When_converting_a_string_to_MD5_hash
    {
        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Should_throw_an_exception_if_the_string_is_null()
        {
            ((string) null).ToMD5();
        }

        [Test]
        public void Should_return_an_MD5_hash_for_a_valid_string()
        {
            string md5 = "andypike".ToMD5();

            Assert.That(md5, Is.EqualTo("8958D00667F8878E3A06BF80F10B2D43"));
        }

        [Test]
        public void Should_return_an_empty_string_if_the_input_is_empty()
        {
            string md5 = "".ToMD5();

            Assert.That(md5, Is.EqualTo(""));
        }
    }
}